﻿using Max.PH5_2008A.ShoppingCode.Domain;
using Max.PH5_2008A.ShoppingCode.IService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Max.PH5_2008A.ShoppingCode.API.Controllers.Product
{
    /// <summary>
    /// 
    /// </summary>
    [Route("product")]
    [ApiController]
    public class ProductController : BaseController<TbProductEntity>
    {
        IProductService _service;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="service"></param>
        public ProductController(IProductService service) : base(service)
        {
            _service = service;
        }

        /*
        * 2023/3/2
        * 要实现三个页面：首页、列表、详情
        * 
        * 分析数据接口
        * 首页：显示推荐、特价、秒杀、专场等商品，后台接口一次性提供需要的数据
        * **/

        /// <summary>
        /// 提供推荐、特价商品（各种只取4个商品）
        /// </summary>
        /// <returns></returns>
        [HttpGet("index")]
        public IActionResult GetIndexData()
        {
            //所有数据
            var data = _service.Query();

            //先查推荐商品4条(标志位第1位为1的，是推荐商品)
            var recommend = data.Where(x => x.Flag.StartsWith("1")).Take(4); //data.Where(x => x.特价字段 == "1").Take(4);  //

            //再查特价商品(标志位第2位为1的，是特价商品)
            var special = data.Where(x => x.Flag.StartsWith("01") || x.Flag.StartsWith("11") ).Take(4);

            //var result = new { type = "1", name = "推荐商品", items = recommend };

            return Ok(new { recommend,special});
        }

        ///// <summary>
        ///// 带分页产品列表显示
        ///// </summary>
        ///// <param name="pageIndex"></param>
        ///// <param name="pageSize"></param>
        ///// <param name="sortField">排序的字段</param>
        ///// <param name="sortValue">排序的值（升还是降）</param>
        ///// <returns></returns>
        //[HttpGet("pages")]
        //public IActionResult QueryPages(int pageIndex,int pageSize,string sortField,string sortValue)
        //{
        //    var data = _service.Query(pageIndex, pageSize, sortField, sortValue, out int count); //_service.Query(pageIndex, pageSize, out int count);

            
        //    //var data = _service.Query(pageIndex, pageSize, x=>x.Price, isDesc, out int count);

        //    var result = new { items = data, total = count };

        //    return Ok(result);
        //}

        /// <summary>
        /// 根据ID获取具体的信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("detail")]
        public IActionResult GetProductInfoById(string id)
        {
            var result = _service.GetProductDetailInfo(id);
            return Ok(result);
        }
    }
}
